package com.step.ems.pages.elevator

import androidx.annotation.StringRes
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.step.ems.R
import com.step.ems.api.ElevatorUseStatus
import com.step.ems.api.res.ElevatorDetailRes
import com.step.ems.component.ExpandableSection
import com.step.ems.component.ItemInfo
import com.step.ems.component.LoadingBox
import com.step.ems.component.MyTopBar
import com.step.ems.ui.theme.ContentBg
import com.step.ems.ui.theme.DividerColor

@Composable
fun ElevatorDetailScreen(
    viewModel: ElevatorDetailViewModel = hiltViewModel(),
) {
    val state by viewModel.uiState.collectAsStateWithLifecycle()
    val data by viewModel.data.collectAsStateWithLifecycle()
    Scaffold(topBar = {
        MyTopBar(title = stringResource(R.string.elevatorDetail))
    }) { padding ->
        LoadingBox(
            modifier = Modifier
                .fillMaxSize()
                .padding(padding),
            state = state
        ) {
            Content(data)
        }
    }
}

@Composable
fun Content(data: ElevatorDetailRes) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(ContentBg)
            .verticalScroll(rememberScrollState())
    ) {
        Spacer(modifier = Modifier.height(12.dp))
        ExpandableSection(title = stringResource(id = R.string.basicInfo), expand = true) {
            Column(
                modifier = Modifier
                    .fillMaxWidth()
            ) {
                ElevatorDetailItem(R.string.elevatorName, data.elevatorVo.name)
                ElevatorDetailItem(R.string.regCode, data.elevatorVo.registrationCode)
                ElevatorDetailItem(R.string.elevatorLocNo, data.elevatorVo.locationCode)
                ElevatorDetailItem(R.string.useState, stringResource(id = ElevatorUseStatus.getTitleRes(data.elevatorVo.useStatus)))
                ElevatorDetailItem(R.string.controlCabinetNo, data.elevatorVo.controlCabinetNo)
                ElevatorDetailItem(R.string.useUnit, data.elevatorVo.useUnitName)
                ElevatorDetailItem(R.string.wbWorker1, data.elevatorVo.maintainerOneName)
                ElevatorDetailItem(R.string.wbWorker2, data.elevatorVo.maintainerTwoName)
                ElevatorDetailItem(R.string.manufacturingCompany, data.elevatorVo.manufacturerName)
                ElevatorDetailItem(R.string.brand, data.elevatorVo.brandName)
            }
        }
    }
}

@Composable
fun ElevatorDetailItem(
    @StringRes titleId: Int,
    value: String
) {
    HorizontalDivider(color = DividerColor)
    ItemInfo(
        titleId,
        value,
        Modifier.padding(horizontal = 18.dp, vertical = 11.dp)
    )
}